package com.leruan.aasimp.dao.impl;

import java.util.List;
import java.util.Map;


import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.dao.CarInsuranceComDao;
import com.leruan.aasimp.entity.CarInsuranceCom;

public class CarInsuranceComDaoImpl extends HibernateDaoSupport implements CarInsuranceComDao{

	@SuppressWarnings("unchecked")
	public List<CarInsuranceCom> findAll() {
		Session session=this.getSession();
		String hql="from CarInsuranceCom where status='1' and type='5'";
		Query query=session.createQuery(hql);
		List<CarInsuranceCom> results=query.list();
		return results;
	}
	@SuppressWarnings("unchecked")
	public List<CarInsuranceCom> getList(Map<String, String> map, int pageNo) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("from CarInsuranceCom where 1 = 1 ");
		if (map.get("status") != null)
			sb.append(" and status=:status ");
		if (map.get("type") != null)
			sb.append(" and type=:type ");
		Query query = session.createQuery(sb.toString());

		if (map.get("status") != null)
			query.setString("status", map.get("status"));
		if (map.get("type") != null)
			query.setString("type", map.get("type"));
		int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
		query.setFirstResult(first);
		query.setMaxResults(UsedCarConstant.PAGE_SIZE);
		List<CarInsuranceCom> results=query.list();
		return results;
	}

	public Long getTotal(Map<String, String> map) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("select count(id) from CarInsuranceCom where 1 = 1 ");
		if (map.get("status") != null)
			sb.append(" and status=:status ");
		if (map.get("type") != null)
			sb.append(" and type=:type ");
		Query query = session.createQuery(sb.toString());
		if (map.get("status") != null)
			query.setString("status", map.get("status"));
		if (map.get("type") != null)
			query.setString("type", map.get("type"));
		
		return (Long)query.uniqueResult();
	}

	public void save(CarInsuranceCom carInsuranceCom) {
		Session session = this.getSession();
		session.save(carInsuranceCom);
	}

	public void update(CarInsuranceCom carInsuranceCom) {
		getHibernateTemplate().update(carInsuranceCom);
	}
	public CarInsuranceCom getComById(Long idCompany) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("from CarInsuranceCom where idCompany =?");
		return (CarInsuranceCom) session.createQuery(sb.toString()).setLong(0, idCompany).uniqueResult();
	}



}
